#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "component.h" // Include C struct definition
#include "supplier.h"  // Include Supplier C struct definition

#include <QLineEdit>   // Include QLineEdit header directly
#include <QLabel>      // For title and labels
#include <QStackedWidget>
#include <QCompleter>       // For search auto-completion
#include <QStringListModel> // For completer model
#include <QMap>             // For supplier ID->Name cache
#include <QListWidget>      // For displaying multiple search results
#include <QtSvgWidgets/QSvgWidget> // For displaying SVG logo

// 向前声明
class QTableWidget;
class QPushButton;
// class QListWidget;     // For navigation - REMOVED
// class QStackedWidget;  // For content area - REMOVED
// class QListWidgetItem; // For list widget item handling - REMOVED
class QTabWidget;      // For tabbed content view
class ComponentDetailPage; // For the new detail page
class SupplierComponentsPage; // For supplier-specific components page

class MainWindow : public QMainWindow {
    Q_OBJECT // Necessary for signals/slots

public:
    MainWindow(QWidget* parent = nullptr);
    ~MainWindow() override;

protected:
    // 重写事件过滤器以捕获 Logo 点击
    bool eventFilter(QObject *watched, QEvent *event) override;
    // 重写按键按下事件以处理 Esc 键
    void keyPressEvent(QKeyEvent *event) override;

private slots:
    // 元器件槽
    void onAddComponent();
    void onEditComponent();
    void onDeleteComponent();
    void onSaveComponents();
    void onReloadComponents();

    // 供应商槽
    void onAddSupplier();
    void onEditSupplier();
    void onDeleteSupplier();
    void onSaveSuppliers();
    void onReloadSuppliers();
    void onSearchQueryChanged(const QString &text); // Slot for search
    void onExecuteSearch();                     // 执行搜索并处理结果
    void showComponentDetailById(int componentId); // 根据ID显示组件详情
    void showSearchPage();                      // 切换到搜索页面
    void showListsPage();                       // 切换到列表查看页面
    void onHotSearchTagClicked(const QString& searchTerm); // 热搜标签点击
    void onViewDetailFromList();                // 从列表页查看详情
    void onSearchResultSelected(QListWidgetItem* item); // 处理多重搜索结果列表的选择
    void onSupplierLogoClicked(int supplierId); // 处理Logo点击事件，准备跳转
    void checkTableAfterPopulation(); // 新增：用于延迟检查表格内容
    // void updateSearchCompleter(const QString& text); // (如果需要)更新搜索自动联想

private:
    // 数据处理和表格填充的辅助方法
    void populateComponentTable(const QString& searchTerm = QString());
    void populateSupplierTable(const QString& searchTerm = QString());
    void loadComponentData();
    void saveComponentData();
    void loadSupplierData();
    void saveSupplierData();

    // UI 元素 - 新布局
    QLabel* titleLabel;          // Main title: "芯片查询工具"
    QLineEdit* mainSearchEdit;   // Central search bar
    QPushButton* searchButton;   // "查询一下" button
    QLabel* hotSearchLabel;      // "热搜型号:" label
    QWidget* hotSearchTagsWidget; // Container for hot search tags (e.g., using QHBoxLayout with QLabels or QPushButtons)

    QStackedWidget* mainStackedWidget;          // 主堆栈窗口，用于切换页面
    ComponentDetailPage* componentDetailPageView; // 元器件详情页实例
    QPushButton* viewAllListsButton;            // “查看列表”按钮 (在搜索页)
    QPushButton* backToSearchButton;            // “返回搜索页”按钮 (在列表页)
    QWidget* searchPageWidget;                  // 搜索主页的容器
    QWidget* listsPageWidget;                   // 列表查看页的容器 (将容纳 contentTabWidget)
    QSvgWidget* infineonLogoWidget;             // Infineon Logo
    QSvgWidget* nxpLogoWidget;                  // NXP Logo
    QSvgWidget* tiLogoWidget;                   // TI Logo
    SupplierComponentsPage* supplierComponentsPageView; // Page for supplier-specific components

    QTabWidget* contentTabWidget; // To hold component and supplier tables

    // --- 元器件标签页的 UI 元素 ---
    // QLineEdit* componentSearchEdit; // REMOVED - replaced by mainSearchEdit or integrated differently
    QTableWidget* componentTable;
    QPushButton *addComponentBtn, *editComponentBtn, *deleteComponentBtn, *viewDetailBtn, *saveComponentsBtn, *reloadComponentsBtn; // Added viewDetailBtn

    // --- 供应商标签页的 UI 元素 ---
    // QLineEdit* supplierSearchEdit; // REMOVED - replaced by mainSearchEdit or integrated differently
    QTableWidget* supplierTable;
    QPushButton *addSupplierBtn, *editSupplierBtn, *deleteSupplierBtn, *saveSuppliersBtn, *reloadSuppliersBtn;

    // 用于搜索 completer 和结果
    QCompleter* searchCompleter;
    QStringListModel* completerModel;
    QListWidget* searchResultList; // List to show multiple search results


    // 数据存储
    ComponentNode* componentHead = nullptr;
    SupplierNode* supplierHead = nullptr;
    QMap<int, QString> supplierNameCache; // Cache for supplier ID to Name lookup
    int populateComponentTableCallCount = 0;
    QString componentFilename = "data/components.dat";
    QString supplierFilename = "data/suppliers.dat";
};

#endif // MAINWINDOW_H